home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / Math / Trig.pm < prev   
Encoding:
Perl POD Document  |  1999-12-28  |  5.2 KB  |  214 lines

  1.  
  2. require Exporter;
  3. package Math::Trig;
  4.  
  5. use strict;
  6.  
  7. use Math::Complex qw(:trig);
  8.  
  9. use vars qw($VERSION $PACKAGE
  10.         @ISA
  11.         @EXPORT);
  12.  
  13. @ISA = qw(Exporter);
  14.  
  15. $VERSION = 1.00;
  16.  
  17. my @angcnv = qw(rad2deg rad2grad
  18.          deg2rad deg2grad
  19.          grad2rad grad2deg);
  20.  
  21. @EXPORT = (@{$Math::Complex::EXPORT_TAGS{'trig'}},
  22.        @angcnv);
  23.  
  24. use constant pi2 => 2 * pi;
  25. use constant DR  => pi2/360;
  26. use constant RD  => 360/pi2;
  27. use constant DG  => 400/360;
  28. use constant GD  => 360/400;
  29. use constant RG  => 400/pi2;
  30. use constant GR  => pi2/400;
  31.  
  32.  
  33. sub remt ($$) {
  34.     $_[0] - $_[1] * int($_[0] / $_[1]);
  35. }
  36.  
  37.  
  38. sub rad2deg ($)  { remt(RD * $_[0], 360) }
  39.  
  40. sub deg2rad ($)  { remt(DR * $_[0], pi2) }
  41.  
  42. sub grad2deg ($) { remt(GD * $_[0], 360) }
  43.  
  44. sub deg2grad ($) { remt(DG * $_[0], 400) }
  45.  
  46. sub rad2grad ($) { remt(RG * $_[0], 400) }
  47.  
  48. sub grad2rad ($) { remt(GR * $_[0], pi2) }
  49.  
  50. =head1 NAME
  51.  
  52. Math::Trig - trigonometric functions
  53.  
  54. =head1 SYNOPSIS
  55.  
  56.     use Math::Trig;
  57.     
  58.     $x = tan(0.9);
  59.     $y = acos(3.7);
  60.     $z = asin(2.4);
  61.     
  62.     $halfpi = pi/2;
  63.  
  64.     $rad = deg2rad(120);
  65.  
  66. =head1 DESCRIPTION
  67.  
  68. C<Math::Trig> defines many trigonometric functions not defined by the
  69. core Perl which defines only the C<sin()> and C<cos()>.  The constant
  70. B<pi> is also defined as are a few convenience functions for angle
  71. conversions.
  72.  
  73. =head1 TRIGONOMETRIC FUNCTIONS
  74.  
  75. The tangent
  76.  
  77.     tan
  78.  
  79. The cofunctions of the sine, cosine, and tangent (cosec/csc and cotan/cot
  80. are aliases)
  81.  
  82.     csc cosec sec cot cotan
  83.  
  84. The arcus (also known as the inverse) functions of the sine, cosine,
  85. and tangent
  86.  
  87.     asin acos atan
  88.  
  89. The principal value of the arc tangent of y/x
  90.  
  91.     atan2(y, x)
  92.  
  93. The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc
  94. and acotan/acot are aliases)
  95.  
  96.     acsc acosec asec acot acotan
  97.  
  98. The hyperbolic sine, cosine, and tangent
  99.  
  100.     sinh cosh tanh
  101.  
  102. The cofunctions of the hyperbolic sine, cosine, and tangent (cosech/csch
  103. and cotanh/coth are aliases)
  104.  
  105.     csch cosech sech coth cotanh
  106.  
  107. The arcus (also known as the inverse) functions of the hyperbolic
  108. sine, cosine, and tangent
  109.  
  110.     asinh acosh atanh
  111.  
  112. The arcus cofunctions of the hyperbolic sine, cosine, and tangent
  113. (acsch/acosech and acoth/acotanh are aliases)
  114.  
  115.     acsch acosech asech acoth acotanh
  116.  
  117. The trigonometric constant B<pi> is also defined.
  118.  
  119.     $pi2 = 2 * pi;
  120.  
  121. =head2 ERRORS DUE TO DIVISION BY ZERO
  122.  
  123. The following functions
  124.  
  125.     tan
  126.     sec
  127.     csc
  128.     cot
  129.     asec
  130.     acsc
  131.     tanh
  132.     sech
  133.     csch
  134.     coth
  135.     atanh
  136.     asech
  137.     acsch
  138.     acoth
  139.  
  140. cannot be computed for all arguments because that would mean dividing
  141. by zero. These situations cause fatal runtime errors looking like this
  142.  
  143.     cot(0): Division by zero.
  144.     (Because in the definition of cot(0), the divisor sin(0) is 0)
  145.     Died at ...
  146.  
  147. For the C<csc>, C<cot>, C<asec>, C<acsc>, C<csch>, C<coth>, C<asech>,
  148. C<acsch>, the argument cannot be C<0> (zero). For the C<atanh>,
  149. C<acoth>, the argument cannot be C<1> (one). For the C<tan>, C<sec>,
  150. C<tanh>, C<sech>, the argument cannot be I<pi/2 + k * pi>, where I<k> is
  151. any integer.
  152.  
  153. =head2 SIMPLE (REAL) ARGUMENTS, COMPLEX RESULTS
  154.  
  155. Please note that some of the trigonometric functions can break out
  156. from the B<real axis> into the B<complex plane>. For example
  157. C<asin(2)> has no definition for plain real numbers but it has
  158. definition for complex numbers.
  159.  
  160. In Perl terms this means that supplying the usual Perl numbers (also
  161. known as scalars, please see L<perldata>) as input for the
  162. trigonometric functions might produce as output results that no more
  163. are simple real numbers: instead they are complex numbers.
  164.  
  165. The C<Math::Trig> handles this by using the C<Math::Complex> package
  166. which knows how to handle complex numbers, please see L<Math::Complex>
  167. for more information. In practice you need not to worry about getting
  168. complex numbers as results because the C<Math::Complex> takes care of
  169. details like for example how to display complex numbers. For example:
  170.  
  171.     print asin(2), "\n";
  172.     
  173. should produce something like this (take or leave few last decimals):
  174.  
  175.     1.5707963267949-1.31695789692482i
  176.  
  177. That is, a complex number with the real part of approximately C<1.571>
  178. and the imaginary part of approximately C<-1.317>.
  179.  
  180. =head1 ANGLE CONVERSIONS
  181.  
  182. (Plane, 2-dimensional) angles may be converted with the following functions.
  183.  
  184.     $radians  = deg2rad($degrees);
  185.     $radians  = grad2rad($gradians);
  186.     
  187.     $degrees  = rad2deg($radians);
  188.     $degrees  = grad2deg($gradians);
  189.     
  190.     $gradians = deg2grad($degrees);
  191.     $gradians = rad2grad($radians);
  192.  
  193. The full circle is 2 I<pi> radians or I<360> degrees or I<400> gradians.
  194.  
  195. =head1 BUGS
  196.  
  197. Saying C<use Math::Trig;> exports many mathematical routines in the
  198. caller environment and even overrides some (C<sin>, C<cos>).  This is
  199. construed as a feature by the Authors, actually... ;-)
  200.  
  201. The code is not optimized for speed, especially because we use
  202. C<Math::Complex> and thus go quite near complex numbers while doing
  203. the computations even when the arguments are not. This, however,
  204. cannot be completely avoided if we want things like C<asin(2)> to give
  205. an answer instead of giving a fatal runtime error.
  206.  
  207. =head1 AUTHORS
  208.  
  209. Jarkko Hietaniemi <F<jhi@iki.fi>> and 
  210. Raphael Manfredi <F<Raphael_Manfredi@grenoble.hp.com>>.
  211.  
  212. =cut
  213.  
  214.